import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


class GuiWindow extends JFrame {
    private GraphPanel graphPanel;
    private ArrayList<JLabel> valueLabelList = new ArrayList<>();
    private final double[] angles = new double[4];

    public GuiWindow() {
        setTitle("曲线系统");
        setSize(800, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        graphPanel = new GraphPanel();
        add(graphPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        JPanel valuePanel = new JPanel();

        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
        valuePanel.setLayout(new GridLayout(4, 1));

        String[] buttonLabels = {"sinx", "cosx", "0.5x", "2x"};
        String[] valueLabels = {"sin函数y的值为 0.0000", "cos函数y的值为 0.0000", "y=0.5x函数y的值为 0.0000", "y=2x函数y的值为 0.0000"};
        for (String buttonLabel : buttonLabels) {
            JButton button = new JButton(buttonLabel);
            button.setPreferredSize(new Dimension(100, 40));
            button.setLayout(new GridLayout(1, 4, 15, 10));
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Timer updateValueTimer = new Timer(5, e -> {
                                updateValue(buttonLabel);
                            });
                            updateValueTimer.start();
                        }
                    });
                    thread.start();
                }
            });
            buttonPanel.add(button);
        }

        for (String valueLabel : valueLabels) {
            JLabel label = new JLabel(valueLabel, SwingConstants.CENTER);
            label.setPreferredSize(new Dimension(250, 30));
            label.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
            valueLabelList.add(label);
            valuePanel.add(label);
        }

        add(buttonPanel, BorderLayout.NORTH);
        add(valuePanel, BorderLayout.EAST);

        Timer updateGraphtTimer = new Timer(1, e ->
        {
            graphPanel.repaintGraph();
            JLabel sinValueLabel = (JLabel) valueLabelList.get(0);
            sinValueLabel.setText(String.format("sin函数y的值为 %.4f", Value.get(0)));
            JLabel cosValueLabel = (JLabel) valueLabelList.get(1);
            cosValueLabel.setText(String.format("cos函数y的值为 %.4f", Value.get(1)));
            JLabel halfValueLabel = (JLabel) valueLabelList.get(2);
            halfValueLabel.setText(String.format("y=0.5x函数y的值为 %.4f", Value.get(2)));
            JLabel twiceValueLabel = (JLabel) valueLabelList.get(3);
            twiceValueLabel.setText(String.format("y=2x函数y的值为 %.4f", Value.get(3)));

        });
        updateGraphtTimer.start();
    }
    private void updateValue(String buttonLabel) {
        switch (buttonLabel) {
            case "sinx": {
                angles[0] += 0.1;
                graphPanel.updateSinGraph(angles[0]);
                break;
            }
            case "cosx": {
                angles[1] += 0.1;
                graphPanel.updateCosGraph(angles[1]);
                break;
            }
            case "0.5x": {
                angles[2] += 0.1;
                graphPanel.updateHalfGraph(angles[2]);
                break;
            }
            case "2x": {
                angles[3] += 0.1;
                graphPanel.updateTwiceGraph(angles[3]);
                break;
            }
        }
    }
}
